const app = getApp()
Page({
  data: {
    content: "",
	files:[],
	sessionFrom:''
  },

  onLoad(options) {
    const newsId = options.id;
    // 模拟从后台获取新闻详情数据
    const newsData = this.loadKeshiData(newsId);
	// this.loadKeshiData()
    var self = this;
    app.util.getUserInfo(function (response) {
        self.setData({
            userInfo: response
        });
        app.mdInit(function (cfg,mine,area) {
            self.setData({
                    cfg: cfg,
                    mine:mine,
                    area:area,
                    tels:area.tels_json?JSON.parse(area.tels_json):'',
    				weixin:area.weixin.split('/'),
                    main_loaded:true
            });       
    		let content = '';
    		if(mine['is_vip'] == 1){
    			content = `当前用户是家庭医生会员，有效期：`+mine['expire_time']+`,请提供优质服务`
    		}else{
    			content = `当前用户是非会员。可向其介绍会员权益进行转化。`
    		}
    		self.setData({
    			sessionFrom: content
    		})
    		setTimeout(() => {
    		  self.selectComponent('#hidden-contact-btn').triggerEvent('tap');
    		}, 100);
        });
    });
  },
  goPreview(event){
    var currentTarget = event.currentTarget; 
    let url=event.currentTarget.dataset.url;
    //如果文件名字没有后缀类型，获取URL的后缀类型
    let name=event.currentTarget.dataset.name.indexOf('.')<0?event.currentTarget.dataset.name+"."+getLastDecimalPart(url,'.'):event.currentTarget.dataset.name;
    wx.downloadFile({
      url: url,
      filePath:wx.env.USER_DATA_PATH+'/'+name,
      success: function (res) {
        const filePath = res.filePath
        wx.openDocument({
          filePath: filePath,
          showMenu:true, //默认是false，为true的时候，右上角有三个点
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      }
    })    
  },
async loadKeshiData(id) {
	  var that = this
    try {
      // 模拟API请求
      app.util.request({
              url: 'Bjjy/ksdetail',
              data: {
				  'id':id
              },
              success(res) {
                var data = res.data.data.bjjy;
				that.setData({
					content: data.content, // 明确设置 content
					files:data.fileData
				  });
              }
      })
    } catch (error) {
      this.setData({ isLoading: false })
      wx.showToast({ title: '加载失败', icon: 'none' })
    }
  },
});